package model
{
	import flash.display.*;
	import flash.events.*;
	import flash.text.*;
	
	public class CustomTabBar extends MovieClip
	{

		private var tabBarBg:TabBarBg;
		private var tabButtonLeft:TabButtonLeft;
		private var tabButtonMiddle:TabButtonMiddle;
		private var tabButtonRight:TabButtonRight;
		private var tabButtonLeft_selected:TabButtonLeft_selected;
		private var tabButtonMiddle_selected:TabButtonMiddle_selected;
		private var tabButtonRight_selected:TabButtonRight_selected;
		private var TabButtonPosY:Number = 7;
		
		private var textLeft:TextField;
		private var textMiddle:TextField;
		private var textRight:TextField;
		
		public function CustomTabBar()
		{
			tabBarBg = new TabBarBg();
			tabButtonLeft = new TabButtonLeft();
			tabButtonMiddle = new TabButtonMiddle();
			tabButtonRight = new TabButtonRight();
			tabButtonLeft_selected = new TabButtonLeft_selected();
			tabButtonMiddle_selected = new TabButtonMiddle_selected();
			tabButtonRight_selected = new TabButtonRight_selected();
			
			textLeft = new TextField();
			textMiddle = new TextField();
			textRight = new TextField();
			tabButtonLeft_selected.width = 225;
			tabButtonLeft.width =  232;
			tabButtonMiddle.width = tabButtonMiddle_selected.width = 232;
			tabButtonRight.width = tabButtonRight_selected.width = 232;
			tabButtonLeft.x = tabButtonLeft_selected.x = 7;			
			tabButtonMiddle.x = tabButtonMiddle_selected.x = tabButtonLeft.width;			
			tabButtonRight.x = tabButtonRight_selected.x = tabButtonMiddle.x + tabButtonMiddle.width;
			
			tabButtonLeft.y = tabButtonLeft_selected.y = TabButtonPosY;
			tabButtonMiddle.y = tabButtonMiddle_selected.y = TabButtonPosY;
			tabButtonRight.y = tabButtonRight_selected.y = TabButtonPosY;
			
			textLeft.x = 80;
			textMiddle.x = 300;
			textRight.x = 530;
			
			textLeft.y = TabButtonPosY + 4;
			textMiddle.y = TabButtonPosY + 4;
			textRight.y = TabButtonPosY + 4;
			
			addChild(tabBarBg);
			addChild(tabButtonLeft);
			addChild(tabButtonMiddle);
			addChild(tabButtonRight);
			addChild(tabButtonLeft_selected);
			addChild(tabButtonMiddle_selected);
			addChild(tabButtonRight_selected);
			
			tabButtonLeft_selected.visible = true;
			tabButtonMiddle_selected.visible = false;
			tabButtonRight_selected.visible = false;
			
			addChild(textLeft);
			addChild(textMiddle);
			addChild(textRight);
			
			setTextFormat(textLeft, "Map");
			setTextFormat(textMiddle, "Satellite");
			setTextFormat(textRight, "Hybrid");
			
			//tabButtonLeft.addEventListener(MouseEvent.CLICK, tabButtonLeftHandler);
			tabButtonMiddle.addEventListener(MouseEvent.CLICK, tabButtonMiddleHandler);
			tabButtonRight.addEventListener(MouseEvent.CLICK, tabButtonRightHandler);
		}
		
		private function tabButtonLeftHandler(event:MouseEvent):void
		{
			tabButtonLeft_selected.visible = true;
			tabButtonMiddle_selected.visible = false;
			tabButtonRight_selected.visible = false;
			tabButtonLeft.removeEventListener(MouseEvent.CLICK, tabButtonLeftHandler);
			tabButtonMiddle.addEventListener(MouseEvent.CLICK, tabButtonMiddleHandler);
			tabButtonRight.addEventListener(MouseEvent.CLICK, tabButtonRightHandler);
			this.dispatchEvent(new Event("MAP_TYPE_SELECTED"));
		}
		
		private function tabButtonMiddleHandler(event:MouseEvent):void
		{
			tabButtonLeft_selected.visible = false;
			tabButtonMiddle_selected.visible = true;
			tabButtonRight_selected.visible = false;
			tabButtonLeft.addEventListener(MouseEvent.CLICK, tabButtonLeftHandler);
			tabButtonMiddle.removeEventListener(MouseEvent.CLICK, tabButtonMiddleHandler);
			tabButtonRight.addEventListener(MouseEvent.CLICK, tabButtonRightHandler);
			this.dispatchEvent(new Event("SATELLITE_TYPE_SELECTED"));
		}
		
		private function tabButtonRightHandler(event:MouseEvent):void
		{
			tabButtonLeft_selected.visible = false;
			tabButtonMiddle_selected.visible = false;
			tabButtonRight_selected.visible = true;
			tabButtonLeft.addEventListener(MouseEvent.CLICK, tabButtonLeftHandler);
			tabButtonMiddle.addEventListener(MouseEvent.CLICK, tabButtonMiddleHandler);
			tabButtonRight.removeEventListener(MouseEvent.CLICK, tabButtonRightHandler);
			this.dispatchEvent(new Event("HYBRID_TYPE_SELECTED"));
		}
		
		private function setTextFormat(txtField:TextField, text:String):void
		{
			txtField.mouseEnabled = false;
			txtField.selectable = false;
			txtField.autoSize = TextFieldAutoSize.CENTER;
			txtField.text = text;
			txtField.setTextFormat(GlobalTextFormat.actionButtonStyle);
		}
		
		override public function set width(value:Number):void
		{			
			tabBarBg.width = value;		
		}
		
		override public function get width():Number
		{
			return tabBarBg.width;
		}
		
		override public function set height(value:Number):void
		{
			tabBarBg.height = value;
		}
		
		override public function get height():Number
		{
			return tabBarBg.height;
		}
	}
}